﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using WFcontrol.GUI;
namespace WFcontrol.BL
{
    class cl_shopcd
    {
        accessdata con;
        Form frm;
        DataTable tb;
        //string SQL;
        public cl_shopcd(frm_shopcd f)
        {
            con = new accessdata();
            frm = f;
        }
        public void refreshdata()
        {
            ((DataGridView)frm.Controls["dataGridview1"]).DataSource = tb;
            ((TextBox)frm.Controls["textBox1"]).DataBindings.Clear();
            ((TextBox)frm.Controls["textBox2"]).DataBindings.Clear();
            ((TextBox)frm.Controls["textBox3"]).DataBindings.Clear();

            //((TextBox)frm.Controls["textBox4"]).DataBindings.Clear();
            ((ComboBox)frm.Controls["ComboBox1"]).DataBindings.Clear();
            ((ComboBox)frm.Controls["ComboBox1"]).DataBindings.Add("Text", tb, "MaDM");
           //((TextBox)frm.Controls["textBox4"]).DataBindings.Add("text", tb, "MaDM");
            ((TextBox)frm.Controls["textBox1"]).DataBindings.Add("text", tb, "MaCD");
            ((TextBox)frm.Controls["textBox2"]).DataBindings.Add("text", tb, "TenLoai");
            ((TextBox)frm.Controls["textBox3"]).DataBindings.Add("text", tb, "Dongiathue");


        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="MaDM"></param>
        public void hienthi2(string MaCD)
        {
            con.Open();
           SqlParameter[] pr = new SqlParameter[1];
            if (MaCD != null)
                pr[0] = new SqlParameter("@MaCD", MaCD);
            else
                pr[0] = new SqlParameter("@MaCD", DBNull.Value);
            tb = con.Execute_ps("ps_laycd", pr);
            refreshdata();
            //       ((DataGridView)frm.Controls["dataGridview1"]).DataSource = tb;
            //     ((TextBox)frm.Controls["textBox1"]).DataBindings.Add("text", tb, "MaDM");
            //   ((TextBox)frm.Controls["textBox2"]).DataBindings.Add("text", tb, "TenDM");
            con.Close();
        }
        public void addcom()
        {
            con.Open();
        SqlParameter[] pr = new SqlParameter[1];
        pr[0] = new SqlParameter("@MaDM", DBNull.Value);
        DataTable tb = con.Execute_ps("ps_layDanhmuc", pr);
        foreach (DataRow f in tb.Rows)
        {
            ((ComboBox)frm.Controls["ComboBox1"]).Items.Add(f["MaDM"]);
        }

        }
        public void set_combobox()
        { 
        
        }
        public void themcd()
        {    
            string macd,tenloai,madm,dongia;
           // madm = ((TextBox)frm.Controls["TextBox4"]).Text;
            macd = ((TextBox)frm.Controls["TextBox1"]).Text;
            madm = ((ComboBox)frm.Controls["ComboBox1"]).Text;
            tenloai = ((TextBox)frm.Controls["TextBox2"]).Text;
            dongia = ((TextBox)frm.Controls["TextBox3"]).Text;
            SqlParameter[] pr = { new SqlParameter("@MaCD", macd), new SqlParameter("@MaDM", madm), new SqlParameter("@Tenloai", tenloai), new SqlParameter("@Dongiathue", dongia) };
            con.Open();
            tb = con.Execute_ps("ps_themcd", pr);
            refreshdata();
            //((DataGridView)frm.Controls["dataGridview1"]).DataSource = tb;

            con.Close();

        }
        public void sua2()
        {
            string macd, tenloai, madm, dongia;
            // madm = ((TextBox)frm.Controls["TextBox4"]).Text;
            macd = ((TextBox)frm.Controls["TextBox1"]).Text;
            madm = ((ComboBox)frm.Controls["ComboBox1"]).Text;
            tenloai = ((TextBox)frm.Controls["TextBox2"]).Text;
            dongia = ((TextBox)frm.Controls["TextBox3"]).Text;
            SqlParameter[] pr = { new SqlParameter("@MaCD", macd), new SqlParameter("@MaDM", madm), new SqlParameter("@Tenloai", tenloai), new SqlParameter("@Dongiathue", dongia) };
            con.Open();
            tb = con.Execute_ps("ps_suacd", pr);
            refreshdata();
            //((DataGridView)frm.Controls["dataGridview1"]).DataSource = tb;

            con.Close();

        }
        public void xoa2()
        {
            string MaCD;
            MaCD = ((TextBox)frm.Controls["TextBox1"]).Text;
            //tendm = ((TextBox)frm.Controls["TextBox2"]).Text;
            // SqlParameter[] pr = { new SqlParameter("@MaDM", madm), new SqlParameter("@TenDM", tendm) };
            SqlParameter[] pr = { new SqlParameter("@MaCD", MaCD) };
            con.Open();
            tb = con.Execute_ps("ps_xoacd", pr);
            //((DataGridView)frm.Controls["dataGridView1"]).DataSource = tb;
            refreshdata();
            con.Close();
        }
       

    }
}
